package com.wn32.main.demo18;

import java.sql.*;

public class IdGenerator {
    public static long nextId() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ids", "root", "root");
            PreparedStatement st = conn.prepareStatement("insert into id_demo_product(createtime) values(now())", Statement.RETURN_GENERATED_KEYS);
            st.executeLargeUpdate();
            ResultSet rs = st.getGeneratedKeys();
            rs.next();
            long id = rs.getLong(1);
            conn.close();
            return id;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    //
}
